package com.graduationpractice.dbot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.graduationpractice.dbot.biz.IOperationlogService;
import com.graduationpractice.dbot.biz.IUsercreatedatabaseService;
import com.graduationpractice.dbot.pojo.Operationlog;
import com.graduationpractice.dbot.pojo.User;


import com.graduationpractice.dbot.pojo.Usercreatedatabase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-03-24
 */
@Controller
@RequestMapping("/usercreatedatabase")
public class UsercreatedatabaseController {
    @Autowired
    IUsercreatedatabaseService iUsercreatedatabaseService;
    @Autowired
    IOperationlogService iOperationlogService;

    @RequestMapping("/createdb")
    public String createDB(HttpSession session, String dbname, String charset) throws SQLException {

        Connection connection = (Connection) session.getAttribute("connection");
        Statement statement = connection.createStatement();

        String createdb = "create database if not exists " + dbname + " character set " + charset;

        List list = (List) session.getAttribute("schema_list");

        if (list.indexOf(dbname) != -1) {
            System.out.println("数据库已存在");

        } else {
            statement.executeUpdate(createdb);
            System.out.println("数据库" + dbname + "创建成功");
            //插入到用户创建数据库表中
            User user1=(User)session.getAttribute("user");
            Usercreatedatabase usercreatedatabase=new Usercreatedatabase();
            usercreatedatabase.setEmail(user1.getEmail());
            usercreatedatabase.setDatabasename(dbname);
            usercreatedatabase.setDatabasetyple(session.getAttribute("databasetype").toString());
            usercreatedatabase.setCreatedate(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
            this.iUsercreatedatabaseService.save(usercreatedatabase);

            //记录日志
            Operationlog op_log=new Operationlog();
            User user=(User) session.getAttribute("user");
            op_log.setEmail(user.getEmail());
            op_log.setDatabasename(dbname);
            op_log.setOperation("创建数据库");
            op_log.setOperationdate(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
            this.iOperationlogService.save(op_log);

        }


//        Response.setContentType("text/html;charset=utf-8");
//        response.getWriter().write("<script>alert('该数据已经同步完成');</script>");
//        response.getWriter().write("<script>alert('网页将跳转到test.do'); window.location='test.do' ;window.close();</script>");
//        response.getWriter().flush();


        statement.close();
        return "redirect:/index";
    }


}

